load('../../src/chrome/content/folderplus-conversation.js');

var aMsgHdr = { messageId : 12,
				folder : { URI : "folder 1" },
				date : new Date(2013,2,9,10,30),
				mime2DecodedSubject : "sujet1" };

var aMsgHdrNoSubject = { messageId : 12,
				folder : { URI : "folder 1" },
				date : new Date(2013,2,9,10,30),
				mime2DecodedSubject : "" };

var aMsgHdrWithPrefix = { messageId : 12,
				folder : { URI : "folder 1" },
				date : new Date(2013,2,9,10,30),
				mime2DecodedSubject : "prefixe : sujet1" };

var callbackCalled   = null;
var callbackUICalled = new Array();

function callback   (aFolderURI) {do_print('fake callback '   + aFolderURI); callbackCalled = aFolderURI;};
function callbackUI (aFolderURI) {do_print('fake callbackui ' + aFolderURI); callbackUICalled.push(aFolderURI);};

function run_test() {
	folderplus.conv._lastSearch.messageId = null;
	folderplus.conv._lastSearch.folderURI = null;
	folderplus.conv._lastSearch.timestamp = null;
  					
	test_1();
	test_12();
	test_13();
	test_14();
	test_2();
	test_22();
	test_222();
	test_223();
	test_23();
	test_3();
}

function test_1() {
	// 2 result by conv / some outside current (nominal case), result OK
	var aGloda = {
		getMessageCollectionForHeader : function (aMsgHdrWithPrefix, aListener) {
			var aGlodaMessage = {
				conversation : {
					getMessagesCollection : function(aConvListener) {
						var aGlodaMessage2  = { folderMessage : { folder : { URI : "folder 2" },
																  date : new Date(2013,2,9,10,30) },
												subject : "sujet1" };
						var aGlodaMessage22 = { folderMessage : { folder : { URI : "folder 2" },
																  date : new Date(2013,2,8,9,0) },
												subject : "sujet2" };
						var aCollection2 = { items : [ aGlodaMessage2, aGlodaMessage22 ] };
						aConvListener.onQueryCompleted(aCollection2);
					}
				}
			};
			var aCollection = { items : [ aGlodaMessage ] };
			aListener.onQueryCompleted(aCollection);
		},
		newQuery : function (aParam) {
			var aQuery = {
				subjectMatches : function(aSubject) {},
				getCollection : function(aSubjectListenerExact) {}
			};
			return aQuery;
		}
	};
	folderplus.conv._getFolder(aMsgHdrWithPrefix, callback, aGloda, callbackUI);
	
	do_check_eq(callbackUICalled[0], "");
	do_check_eq(callbackCalled,      "folder 2");
	do_check_eq(callbackUICalled[1], "folder 2");

}

function test_14() {
	// 2 result by conv / all in current folder, no subject, no result
	
	callbackCalled   = null;
	callbackUICalled = new Array();

	folderplus.conv._lastSearch.messageId = null;
	folderplus.conv._lastSearch.folderURI = null;
	folderplus.conv._lastSearch.timestamp = null;

	var aGloda = {
		getMessageCollectionForHeader : function (aMsgHdrWithPrefix, aListener) {
			var aGlodaMessage = {
				conversation : {
					getMessagesCollection : function(aConvListener) {
						var aGlodaMessage2  = { folderMessage : { folder : { URI : "folder 1" },
																  date : new Date(2013,2,9,10,30) },
												subject : "sujet1" };
						var aGlodaMessage22 = { folderMessage : { folder : { URI : "folder 1" },
																  date : new Date(2013,2,8,9,0) },
												subject : "sujet2" };
						var aCollection2 = { items : [ aGlodaMessage2, aGlodaMessage22 ] };
						aConvListener.onQueryCompleted(aCollection2);
					}
				}
			};
			var aCollection = { items : [ aGlodaMessage ] };
			aListener.onQueryCompleted(aCollection);
		},
		newQuery : function (aParam) {
			var aQuery = {
				subjectMatches : function(aSubject) {},
				getCollection : function(aSubjectListenerExact) {}
			};
			return aQuery;
		}
	};
	folderplus.conv._getFolder(aMsgHdrNoSubject, callback, aGloda, callbackUI);
	
	do_check_eq(callbackUICalled[0], "");
	do_check_eq(callbackCalled,      null);
	do_check_eq(callbackUICalled[1], null);

}

function test_12() {
	// 1 result by conv, no subject to use, no result
	
	callbackCalled   = null;
	callbackUICalled = new Array();

	folderplus.conv._lastSearch.messageId = null;
	folderplus.conv._lastSearch.folderURI = null;
	folderplus.conv._lastSearch.timestamp = null;

	var aGloda = {
		getMessageCollectionForHeader : function (aMsgHdr, aListener) {
			var aGlodaMessage = {
				conversation : {
					getMessagesCollection : function(aConvListener) {
						var aGlodaMessage2  = { folderMessage : { folder : { URI : "folder 1" },
																  date : new Date(2013,2,9,10,30) },
												subject : "sujet1" };
						var aCollection2 = { items : [ aGlodaMessage2 ] };
						aConvListener.onQueryCompleted(aCollection2);
					}
				}
			};
			var aCollection = { items : [ aGlodaMessage ] };
			aListener.onQueryCompleted(aCollection);
		},
		newQuery : function (aParam) {
			var aQuery = {
				subjectMatches : function(aSubject) {},
				getCollection : function(aSubjectListenerExact) {}
			};
			return aQuery;
		}
	};
	folderplus.conv._getFolder(aMsgHdrNoSubject, callback, aGloda, callbackUI);
	
	do_check_eq(callbackUICalled[0], "");
	do_check_eq(callbackCalled,      null);
	do_check_eq(callbackUICalled[1], null);

}

function test_13() {
	// 0 result by conv, error
	
	callbackCalled   = null;
	callbackUICalled = new Array();

	folderplus.conv._lastSearch.messageId = null;
	folderplus.conv._lastSearch.folderURI = null;
	folderplus.conv._lastSearch.timestamp = null;

	var aGloda = {
		getMessageCollectionForHeader : function (aMsgHdr, aListener) {
			var aGlodaMessage = {
				conversation : {
					getMessagesCollection : function(aConvListener) {
						var aCollection2 = { items : [  ] };
						aConvListener.onQueryCompleted(aCollection2);
					}
				}
			};
			var aCollection = { items : [ aGlodaMessage ] };
			aListener.onQueryCompleted(aCollection);
		},
		newQuery : function (aParam) {
			var aQuery = {
				subjectMatches : function(aSubject) {},
				getCollection : function(aSubjectListenerExact) {}
			};
			return aQuery;
		}
	};
	folderplus.conv._getFolder(aMsgHdrNoSubject, callback, aGloda, callbackUI);
	
	do_check_eq(callbackUICalled[0], "");
	do_check_eq(callbackCalled,      "error");
	do_check_eq(callbackUICalled[1], "error");

}

function test_2 () {
	// 1 result by conv, 2 result by prefix / some outside current folder, result OK (nominal by prefix)
	
	callbackCalled   = null;
	callbackUICalled = new Array();

	folderplus.conv._lastSearch.messageId = null;
	folderplus.conv._lastSearch.folderURI = null;
	folderplus.conv._lastSearch.timestamp = null;

	var aGloda = {
		getMessageCollectionForHeader : function (aMsgHdrWithPrefix, aListener) {
			var aGlodaMessage = {
				conversation : {
					getMessagesCollection : function(aConvListener) {
						var aGlodaMessage2  = { folderMessage : { folder : { URI : "folder 1" },
																  date : new Date(2013,2,9,10,30) },
												subject : "sujet1" };
						var aCollection2 = { items : [ aGlodaMessage2 ] };
						aConvListener.onQueryCompleted(aCollection2);
					}
				}
			};
			var aCollection = { items : [ aGlodaMessage ] };
			aListener.onQueryCompleted(aCollection);
		},
		newQuery : function (aParam) {
			var aQuery = {
				subjectMatches : function(aSubject) {},
				getCollection : function(aSubjectListenerExact) {
					var aGlodaMessage  = { folderMessage : { folder : { URI : "folder 3" },
															  date : new Date(2013,2,8,10,30) },
										   subject : "sujet3" };
					var aGlodaMessage2  = { folderMessage : { folder : { URI : "folder 3" },
															  date : new Date(2013,2,7,10,30) },
											subject : "re: sujet3" };
					var aCollection = { items : [ aGlodaMessage, aGlodaMessage2 ] };
					aSubjectListenerExact.onQueryCompleted(aCollection);
				}
			};
			return aQuery;
		},
		subjectMatches : function (aSubject) {},
	};
	folderplus.conv._getFolder(aMsgHdrWithPrefix, callback, aGloda, callbackUI);
	
	do_check_eq(callbackUICalled[0], "");
	do_check_eq(callbackCalled,      "folder 3");
	do_check_eq(callbackUICalled[1], "folder 3");
}

function test_22 () {
	// search by subject => no result, can not search by prefix, result = null (no exception)
	// 1 result by conv, 0 result by conv, no prefix, 0 result by prefix
	callbackCalled   = null;
	callbackUICalled = new Array();

	folderplus.conv._lastSearch.messageId = null;
	folderplus.conv._lastSearch.folderURI = null;
	folderplus.conv._lastSearch.timestamp = null;

	var aGloda = {
		getMessageCollectionForHeader : function (aMsgHdrWithPrefix, aListener) {
			var aGlodaMessage = {
				conversation : {
					getMessagesCollection : function(aConvListener) {
						var aGlodaMessage2  = { folderMessage : { folder : { URI : "folder 1" },
																  date : new Date(2013,2,9,10,30) },
												subject : "sujet1" };
						var aCollection2 = { items : [ aGlodaMessage2 ] };
						aConvListener.onQueryCompleted(aCollection2);
					}
				}
			};
			var aCollection = { items : [ aGlodaMessage ] };
			aListener.onQueryCompleted(aCollection);
		},
		newQuery : function (aParam) {
			var aQuery = {
				subjectMatches : function(aSubject) {},
				getCollection : function(aSubjectListenerExact) {
					var aCollection = { items : [ ] };
					aSubjectListenerExact.onQueryCompleted(aCollection);
				}
			};
			return aQuery;
		},
		subjectMatches : function (aSubject) {},
	};
	folderplus.conv._getFolder(aMsgHdr, callback, aGloda, callbackUI);
	
	do_check_eq(callbackUICalled[0], "");
	do_check_eq(callbackCalled,      null);
	do_check_eq(callbackUICalled[1], null);
}

function test_222 () {
	// search by subject => 1 result, can not search by prefix, result = null (no exception)
	// 1 result by conv, 1 result by conv, no prefix, 0 result by prefix
	callbackCalled   = null;
	callbackUICalled = new Array();

	folderplus.conv._lastSearch.messageId = null;
	folderplus.conv._lastSearch.folderURI = null;
	folderplus.conv._lastSearch.timestamp = null;

	var aGloda = {
		getMessageCollectionForHeader : function (aMsgHdrWithPrefix, aListener) {
			var aGlodaMessage = {
				conversation : {
					getMessagesCollection : function(aConvListener) {
						var aGlodaMessage2  = { folderMessage : { folder : { URI : "folder 1" },
																  date : new Date(2013,2,9,10,30) },
												subject : "sujet1" };
						var aCollection2 = { items : [ aGlodaMessage2 ] };
						aConvListener.onQueryCompleted(aCollection2);
					}
				}
			};
			var aCollection = { items : [ aGlodaMessage ] };
			aListener.onQueryCompleted(aCollection);
		},
		newQuery : function (aParam) {
			var aQuery = {
				subjectMatches : function(aSubject) {},
				getCollection : function(aSubjectListenerExact) {
					var aGlodaMessage  = { folderMessage : { folder : { URI : "folder 3" },
															  date : new Date(2013,2,8,10,30) },
										   subject : "sujet3" };
					var aCollection = { items : [ aGlodaMessage ] };
					aSubjectListenerExact.onQueryCompleted(aCollection);
				}
			};
			return aQuery;
		},
		subjectMatches : function (aSubject) {},
	};
	folderplus.conv._getFolder(aMsgHdr, callback, aGloda, callbackUI);
	
	do_check_eq(callbackUICalled[0], "");
	do_check_eq(callbackCalled,      null);
	do_check_eq(callbackUICalled[1], null);
}

function test_223 () {
	// search by subject => 2 result/all in current folder, can not search by prefix, result = null (no exception)
	// 1 result by conv, 2 result by conv / all in current folder, no prefix, 0 result by prefix
	callbackCalled   = null;
	callbackUICalled = new Array();

	folderplus.conv._lastSearch.messageId = null;
	folderplus.conv._lastSearch.folderURI = null;
	folderplus.conv._lastSearch.timestamp = null;

	var aGloda = {
		getMessageCollectionForHeader : function (aMsgHdrWithPrefix, aListener) {
			var aGlodaMessage = {
				conversation : {
					getMessagesCollection : function(aConvListener) {
						var aGlodaMessage2  = { folderMessage : { folder : { URI : "folder 1" },
																  date : new Date(2013,2,9,10,30) },
												subject : "sujet1" };
						var aCollection2 = { items : [ aGlodaMessage2 ] };
						aConvListener.onQueryCompleted(aCollection2);
					}
				}
			};
			var aCollection = { items : [ aGlodaMessage ] };
			aListener.onQueryCompleted(aCollection);
		},
		newQuery : function (aParam) {
			var aQuery = {
				subjectMatches : function(aSubject) {},
				getCollection : function(aSubjectListenerExact) {
					var aGlodaMessage  = { folderMessage : { folder : { URI : "folder 1" },
															  date : new Date(2013,2,8,10,30) },
										   subject : "sujet3" };
					var aGlodaMessage2  = { folderMessage : { folder : { URI : "folder 1" },
															  date : new Date(2013,2,8,11,00) },
										   subject : "re: sujet3" };
					var aCollection = { items : [ aGlodaMessage, aGlodaMessage2 ] };
					aSubjectListenerExact.onQueryCompleted(aCollection);
				}
			};
			return aQuery;
		},
		subjectMatches : function (aSubject) {},
	};
	folderplus.conv._getFolder(aMsgHdr, callback, aGloda, callbackUI);
	
	do_check_eq(callbackUICalled[0], "");
	do_check_eq(callbackCalled,      null);
	do_check_eq(callbackUICalled[1], null);
}

function test_23 () {
	// 1 result by conv, 1 result by subject, prefix usable, 0 result by prefix, no result
	callbackCalled   = null;
	callbackUICalled = new Array();

	folderplus.conv._lastSearch.messageId = null;
	folderplus.conv._lastSearch.folderURI = null;
	folderplus.conv._lastSearch.timestamp = null;

	var aGloda = {
		getMessageCollectionForHeader : function (aMsgHdrWithPrefix, aListener) {
			var aGlodaMessage = {
				conversation : {
					getMessagesCollection : function(aConvListener) {
						var aGlodaMessage2  = { folderMessage : { folder : { URI : "folder 1" },
																  date : new Date(2013,2,9,10,30) },
												subject : "sujet1" };
						var aCollection2 = { items : [ aGlodaMessage2 ] };
						aConvListener.onQueryCompleted(aCollection2);
					}
				}
			};
			var aCollection = { items : [ aGlodaMessage ] };
			aListener.onQueryCompleted(aCollection);
		},
		newQuery : function (aParam) {
			var aQuery = {
				subjectMatches : function(aSubject) {},
				getCollection : function(aSubjectListenerExact) {
					var aCollection = { items : [ ] };
					aSubjectListenerExact.onQueryCompleted(aCollection);
				}
			};
			var aQuery2 = {
				subjectMatches : function(aSubject) {},
				getCollection : function(aSubjectListenerExact) {
					var aCollection = { items : [  ] };
					aSubjectListenerExact.onQueryCompleted(aCollection);
				}
			}
			if (this.newQueryCall == 0) {
				this.newQueryCall++;
				return aQuery;	
			} else {
				return aQuery2;
			}
		},
		subjectMatches : function (aSubject) {},
	};
	folderplus.conv._getFolder(aMsgHdrWithPrefix, callback, aGloda, callbackUI);
	
	do_check_eq(callbackUICalled[0], "");
	do_check_eq(callbackCalled,      null);
	do_check_eq(callbackUICalled[1], null);
}

function test_3 () {
	// nominal case by prefix
	// 1 result by conv, 1 result by subject, prefix usable, 1 result by prefix, result ok	
	
	callbackCalled   = null;
	callbackUICalled = new Array();

	folderplus.conv._lastSearch.messageId = null;
	folderplus.conv._lastSearch.folderURI = null;
	folderplus.conv._lastSearch.timestamp = null;

	var aGloda = {
		getMessageCollectionForHeader : function (aMsgHdrWithPrefix, aListener) {
			var aGlodaMessage = {
				conversation : {
					getMessagesCollection : function(aConvListener) {
						var aGlodaMessage2  = { folderMessage : { folder : { URI : "folder 1" },
																  date : new Date(2013,2,9,10,30) },
												subject : "sujet1" };
						var aCollection2 = { items : [ aGlodaMessage2 ] };
						aConvListener.onQueryCompleted(aCollection2);
					}
				}
			};
			var aCollection = { items : [ aGlodaMessage ] };
			aListener.onQueryCompleted(aCollection);
		},
		newQueryCall : 0,
		newQuery : function (aParam) {
			var aQuery = {
				subjectMatches : function(aSubject) {},
				getCollection : function(aSubjectListenerExact) {
					var aGlodaMessage  = { folderMessage : { folder : { URI : "folder 3" },
															  date : new Date(2013,2,8,10,30) },
										   subject : "sujet3" };
					var aCollection = { items : [ aGlodaMessage ] };
					aSubjectListenerExact.onQueryCompleted(aCollection);
				},
			};
			var aQuery2 = {
				subjectMatches : function(aSubject) {},
				getCollection : function(aSubjectListenerExact) {
					var aGlodaMessage  = { folderMessage : { folder : { URI : "folder 3" },
															  date : new Date(2013,2,8,10,30) },
										   subject : "sujet3" };
					var aCollection = { items : [ aGlodaMessage ] };
					aSubjectListenerExact.onQueryCompleted(aCollection);
				}
			}
			if (this.newQueryCall == 0) {
				this.newQueryCall++;
				return aQuery;	
			} else {
				return aQuery2;
			}
		},
		subjectMatches : function (aSubject) {},
	};
	folderplus.conv._getFolder(aMsgHdrWithPrefix, callback, aGloda, callbackUI);
	
	do_check_eq(callbackUICalled[0], "");
	do_check_eq(callbackCalled,      "folder 3");
	do_check_eq(callbackUICalled[1], "folder 3");
}

folderplus.log = function (arg) {do_print(arg);};

function setTimeout(func, delay) {}

